package com.antler.codelua.view;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;

public abstract class BaseFloatDragView extends BaseFloatView {
    private static final String TAG = "antler_" + BaseFloatDragView.class.getSimpleName();

    protected float mXDownInView;
    protected float mYDownInView;

    public BaseFloatDragView(Context context) {
        super(context);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        float x = event.getRawX();
        float y = event.getRawY();
        float xInView = event.getX();
        float yInView = event.getY();

        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "x=" + x + " y=" + y + " xInView=" + xInView + " yInView=" + yInView);
                mXDownInView = x;
                mYDownInView = y;
                actionDown(event);
                break;
            case MotionEvent.ACTION_UP:
                actionUp(event);
                break;
            case MotionEvent.ACTION_MOVE:
                int offsetX = (int) (x - mXDownInView);
                int offsetY = (int) (y - mYDownInView);

                Log.d(TAG, "offsetX=" + offsetX + " offsetY=" + offsetY);

                offsetViewPosition(offsetX, offsetY);

                mXDownInView = x;
                mYDownInView = y;

                actionMove(event);
                break;
        }
        return super.dispatchTouchEvent(event);
    }
}
